package com.bj58.groupbuy.action.controllers;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.wuba.WAQ;

import com.bj58.groupbuy.action.charts.ChartService;
import com.bj58.groupbuy.action.model.BelongLocal;
import com.bj58.groupbuy.action.model.ChinahrIndustry;
import com.bj58.groupbuy.action.model.ChinahrJob;
import com.bj58.groupbuy.action.model.ChinahrSource;
import com.bj58.groupbuy.action.model.DisplayLocal;
import com.bj58.groupbuy.action.model.InfoSourcePlatform;
import com.bj58.groupbuy.action.model.JxedtFlowSource;
import com.bj58.groupbuy.action.model.NetFlowSource;
import com.bj58.groupbuy.action.model.NewBelongCate;
import com.bj58.groupbuy.action.model.NewDisplayCate;
import com.bj58.groupbuy.action.util.MyServletRequestUtils;
import com.bj58.groupbuy.util.IOUtils;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

@Path("/intChartsTool")
public class IntChartsToolsController extends MvcController{
	ChartService chartService = new ChartService();
	
	/**展现类别 - 级联查询 （新）
	 * @return
	 */
	@Path("/newdiscate")
	@POST
	public ActionResult searchNewDisplayCate() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String business_type_name = MyServletRequestUtils.getStringParameter(request, "business_type_name","");
				business_type_name = WAQ.forSQL().escapeSql(business_type_name );

				String cate1 = MyServletRequestUtils.getStringParameter(request, "cate1","");
				
				String cate2 = MyServletRequestUtils.getStringParameter(request, "cate2","");
				
				String isBizValue = MyServletRequestUtils.getStringParameter(request, "isBiz","false");
				int level = MyServletRequestUtils.getIntParameter(request, "level", 0);
				
				boolean isBiz = false;
				if("true".equals(isBizValue)){
					isBiz = true;
				}
				try {
					JSONObject json = new JSONObject();
					List<NewDisplayCate> disCateListOne =  new ArrayList<NewDisplayCate>();//1级列表
					List<NewDisplayCate> disCateListTwo =  new ArrayList<NewDisplayCate>();//2级列表
					List<NewDisplayCate> disCateListThree =  new ArrayList<NewDisplayCate>();//3级列表
					String result = "right";
					if(level == 1){  //频道 、 1级 级联
						disCateListOne = chartService.getLevelOne(business_type_name ,cate1, isBiz);
					}else if( level == 2 ){  //1、2级联
						disCateListTwo = chartService.getLevelTwo(business_type_name,cate1,cate2, isBiz);//2级
					}else if(level == 3){//1、2、3级联
						disCateListThree = chartService.getLevelThree(business_type_name,cate1, cate2,null, isBiz);
					}else{
						result = "error";
					}
					json.accumulate("disCateListOne", disCateListOne);
					json.accumulate("disCateListTwo", disCateListTwo);
					json.accumulate("disCateListThree", disCateListThree);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
	/**归属类别 - 级联查询 （新）
	 * @return
	 */
	@Path("/newbelongcate")
	@POST
	public ActionResult searchNewBLCate() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String business_type_name = MyServletRequestUtils.getStringParameter(request, "business_type_name","");
				business_type_name = WAQ.forSQL().escapeSql(business_type_name );

				String cate1 = MyServletRequestUtils.getStringParameter(request, "cate1","");
				
				String cate2 = MyServletRequestUtils.getStringParameter(request, "cate2","");
				
				String isBizValue = MyServletRequestUtils.getStringParameter(request, "isBiz","false");
				int level = MyServletRequestUtils.getIntParameter(request, "level", 0);
				
				boolean isBiz = false;
				if("true".equals(isBizValue)){
					isBiz = true;
				}
				try {
					JSONObject json = new JSONObject();
					List<NewBelongCate> disCateListOne =  new ArrayList<NewBelongCate>();//1级列表
					List<NewBelongCate> disCateListTwo =  new ArrayList<NewBelongCate>();//2级列表
					List<NewBelongCate> disCateListThree =  new ArrayList<NewBelongCate>();//3级列表
					String result = "right";
					if(level == 1){  //频道 、 1级 级联
						disCateListOne = chartService.getBLLevelOne(business_type_name ,cate1, isBiz);
					}else if( level == 2 ){  //1、2级联
						disCateListTwo = chartService.getBLLevelTwo(business_type_name,cate1, cate2, isBiz);//2级
					}else if(level == 3){//1、2、3级联
						disCateListThree = chartService.getBLLevelThree(business_type_name,cate1, cate2, null,isBiz);
					}else{
						result = "error";
					}
					json.accumulate("disCateListOne", disCateListOne);
					json.accumulate("disCateListTwo", disCateListTwo);
					json.accumulate("disCateListThree", disCateListThree);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
	/**展现城市 - 级联查询
	 * @return
	 */
	@Path("/displaycity")
	@POST
	public ActionResult searchDisplayCity() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				int level = MyServletRequestUtils.getIntParameter(request, "level", 0);
				String city1 = MyServletRequestUtils.getStringParameter(request, "city1","");
				String city2 = MyServletRequestUtils.getStringParameter(request, "city2","");
				try {
					JSONObject json = new JSONObject();
					List<DisplayLocal> disCityListTwo =  new ArrayList<DisplayLocal>();//2级列表
					List<DisplayLocal> disCityListThree =  new ArrayList<DisplayLocal>();//3级列表
					String result = "right";
					if(level == 2){  //1、2级联
						disCityListTwo = chartService.getDisplayLevelTwo(city1, null);//2级
					}if(level == 3){//1、2、3级联
						disCityListThree = chartService.getDisplayLevelThree(city1, city2,null);
					}else{
						result = "error";
					}
					json.accumulate("disCityListTwo", disCityListTwo);
					json.accumulate("disCityListThree", disCityListThree);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	/**归属城市 - 级联查询
	 * @return
	 */
	@Path("belongcity")
	@POST
	public ActionResult searchBelongCity() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				int level = MyServletRequestUtils.getIntParameter(request, "level", 0);
				String city1 = MyServletRequestUtils.getStringParameter(request, "city1","");
				String city2 = MyServletRequestUtils.getStringParameter(request, "city2","");
				try {
					JSONObject json = new JSONObject();
					List<BelongLocal> belongCityListTwo =  new ArrayList<BelongLocal>();//2级列表
					List<BelongLocal> belongCityListThree =  new ArrayList<BelongLocal>();//3级列表
					String result = "right";
					if(level == 2){  //1、2级联
						belongCityListTwo =chartService.getBelongLevelTwo(city1,null);//2级
					}else if(level == 3){//1、2、3级联
						belongCityListThree = chartService.getBelongLevelThree(city1, city2,null);
					}else{
						result = "error";
					}
					json.accumulate("belongCityListTwo", belongCityListTwo);
					json.accumulate("belongCityListThree", belongCityListThree);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	/** 流量来源 - 级联查询
	 * @return
	 */
	@Path("flowsourcesearch")
	@POST
	public ActionResult flowsourcesearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				int level = MyServletRequestUtils.getIntParameter(request, "level", 0);
				String flowSourcePlatform = MyServletRequestUtils.getStringParameter(request, "flowSourcePlatform","");
				String source1 = MyServletRequestUtils.getStringParameter(request, "source1","");
				String source2 = MyServletRequestUtils.getStringParameter(request, "source2","");
				String source3 = MyServletRequestUtils.getStringParameter(request, "source3","");
				try {
					JSONObject json = new JSONObject();
					List<NetFlowSource> flowSourceList =  new ArrayList<NetFlowSource>();//2级列表
					String result = "right";
					if(level == 2){  //1、2级联
						flowSourceList =chartService.getSource2List(flowSourcePlatform,source1,null);//2级
					}else if(level == 3){
						flowSourceList = chartService.getSource3List(flowSourcePlatform,source1, source2,null);
					}else if(level == 3){
						flowSourceList = chartService.getSource4List(source1, source2,source3,null);
					}else{
						result = "error";
					}
					json.accumulate("flowSourceList", flowSourceList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	/** 信息来源级联查询
	 * @return
	 */
	@Path("infosourceSearch")
	@POST
	public ActionResult infosourceSearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String infoPlatform = MyServletRequestUtils.getStringParameter(request, "infoPlatform","");
				try {
					JSONObject json = new JSONObject();
					List<InfoSourcePlatform> sourceList =  new ArrayList<InfoSourcePlatform>();//2级列表
					String result = "right";
					sourceList = chartService.getInfoSourceList(infoPlatform,null);
					json.accumulate("infoSourceList", sourceList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
	/**驾校一点通流量source级联
	 * @return
	 */
	@Path("jxedtSourceSearch")
	@POST
	public ActionResult jxedtSourceSearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String source1 = MyServletRequestUtils.getStringParameter(request, "source1","");
				String pathId = MyServletRequestUtils.getStringParameter(request, "pathId","");
				String platform = MyServletRequestUtils.getStringParameter(request, "jxedtFlowSourcePlatform","");
				//根据pathId来判定是PC还是M的报表
				if(StringUtils.isBlank(platform)){
					if (pathId.contains("359")) {
						platform = "PC";
					} else if (pathId.contains("360")) {
						platform = "M";
					}
				}
				try {
					JSONObject json = new JSONObject();
					List<JxedtFlowSource> jxedtSourceList =  new ArrayList<JxedtFlowSource>();
					String result = "right";
					jxedtSourceList = chartService.getJxedtSource2List(platform, source1, null);
					json.accumulate("jxedtSourceList", jxedtSourceList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}	

	
	/**中华英才流量source级联
	 * @return
	 */
	@Path("chrflowsourcesearch")
	@POST
	public ActionResult chrFlowSourceSearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				int level = MyServletRequestUtils.getIntParameter(request, "level", 0);
				String source1 = MyServletRequestUtils.getStringParameter(request, "source1","");
				String source2 = MyServletRequestUtils.getStringParameter(request, "source2","");
				String source3 = MyServletRequestUtils.getStringParameter(request, "source3","");
				String pathId = MyServletRequestUtils.getStringParameter(request, "pathId","");
				String platform = MyServletRequestUtils.getStringParameter(request, "jxedtFlowSourcePlatform","");
				//根据pathId来判定是PC还是M的报表
				if(StringUtils.isBlank(platform)){	
					//根据pathId来判定是PC还是M的报表
					if(pathId.contains("382")){
						platform = "PC";
					}else if(pathId.contains("397")){
						platform = "M";
					}
				}
				try {
					JSONObject json = new JSONObject();
					List<ChinahrSource> chrSourceList =  new ArrayList<ChinahrSource>();//2级列表
					
					String result = "right";
					if(level == 2){  //1-2级联
						chrSourceList = chartService.getCHRSource2(platform, source1, null);
					}else if (level == 3){  //2-3级联
						chrSourceList = chartService.getCHRSource3(platform, source1, source2, null);
					}else if (level == 4){  //3-4级联
						chrSourceList = chartService.getCHRSource4(platform, source1, source2, source3,null);
					}
					
					else{
						result = "error";
					}
					json.accumulate("chrSourceList", chrSourceList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}


	/**中华英才-行业级联
	 * @return
	 */
	@Path("chrindustrysearch")
	@POST
	public ActionResult chrIndustrySearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String source1 = MyServletRequestUtils.getStringParameter(request, "source1","");
				try {
					JSONObject json = new JSONObject();
					List<ChinahrIndustry> chrIndustryList =  new ArrayList<ChinahrIndustry>();
					String result = "right";
					chrIndustryList = chartService.getgetCHRIndustryCate2(source1, null);
					json.accumulate("chrIndustryList", chrIndustryList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}

	
	

	/**中华英才职位级联
	 * @return
	 */
	@Path("chrjobsearch")
	@POST
	public ActionResult chrjobsearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String cate1_name = MyServletRequestUtils.getStringParameter(request, "cate1_name","");
				String cate2_name = MyServletRequestUtils.getStringParameter(request, "cate2_name","");
				int level = MyServletRequestUtils.getIntParameter(request, "level", 0);

				try {
					JSONObject json = new JSONObject();
					List<ChinahrJob> chrJobList =  new ArrayList<ChinahrJob>();
					
					String result = "right";
					if(level == 2){  //1-2级联
						chrJobList = chartService.getCHRJobCate2(cate1_name, null);
					}else if (level == 3){  //2-3级联
						chrJobList = chartService.getCHRJobCate3(cate1_name, cate2_name, null);
					} else{
						result = "error";
					}
					json.accumulate("chrJobList", chrJobList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}


}
